home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_4.lha / 6_4 / 6_4.h next >
C/C++ Source or Header  |  1993-08-08  |  2KB  |  115 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Exercise 6.4
  6. / String class with value semantics
  7. / and copying on assignment.
  8. ifndef STR_H
  9.  define STR_H
  10.  include <stream.h>
  11.  include <string.h>
  12.  
  13. / a helper typedef for the cast
  14. / back into a character pointer
  15. ypedef const char *charptr;
  16.  
  17. lass string
  18.  
  19.    char *p;
  20.    int size;
  21.  
  22. ublic:
  23.    // string s;
  24.    string()
  25.    {
  26. p = new char[size = 1];
  27. p[0] = 0;
  28.    }
  29.  
  30.    // string s(5);
  31.    string(int sz)
  32.    {
  33. if (sz <= 0) sz = 1;
  34. p = new char[size = sz];
  35. p[0] = 0;
  36.    }
  37.  
  38.    // string s("xyz");
  39.    string(char *s)
  40.    {
  41. p = new char[size = strlen(s) + 1];
  42. strcpy(p, s);
  43.    }
  44.  
  45.    // string s = string
  46.    string &operator=(string &s)
  47.    {
  48. delete p;
  49. p = new char[size = s.size];
  50. strcpy(p, s.p);
  51. return *this;
  52.    }
  53.  
  54.    // string s = "xyz";
  55.    string &operator=(char *s)
  56.    {
  57. delete p;
  58. p = new char[size = strlen(s) + 1];
  59. strcpy(p, s);
  60. return *this;
  61.    }
  62.  
  63.    ~string()
  64.    { delete p; }
  65.  
  66.    // const char *c = s;
  67.    charptr operator charptr()
  68.    { return (charptr)p; }
  69.  
  70.    // x = s[3];
  71.    // s[3] = 'x';
  72.    char &operator[](int i)
  73.    {
  74. if (i < 0 || i >= size)
  75.     return p[0];
  76. else
  77.     return p[i];
  78.    }
  79.  
  80.    friend ostream& operator<<(ostream&, string&);
  81.    friend istream& operator>>(istream&, string&);
  82.  
  83.    friend int operator==(string &x, char *s)
  84.    { return strcmp(x.p, s) == 0; }
  85.  
  86.    friend int operator==(string &x, string &y)
  87.    { return strcmp(x.p, y.p) == 0; }
  88.  
  89.    friend int operator!=(string &x, char *s)
  90.    { return strcmp(x.p, s) != 0; }
  91.  
  92.    friend int operator!=(string &x, string &y)
  93.    { return strcmp(x.p, y.p) != 0; }
  94.                          // DELETE
  95.    void outputstring()                     // DELETE
  96.    {                             // DELETE
  97. cout << "\tsize = " << size << "\n";         // DELETE
  98. cout << "\tp = '" << p << "'\n";         // DELETE
  99.    }                             // DELETE
  100. ;
  101.  
  102. nline ostream& operator<<(ostream &s, string &x)
  103.  
  104.    return s << x.p;
  105.  
  106.  
  107. nline istream& operator>>(istream &s, string &x)
  108.  
  109.    char buf[1024];
  110.    s >> buf;
  111.    x = buf;
  112.    return s;
  113.  
  114. endif /* STR_H */
  115.